<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item   label="门店名称" prop="storeId">
        <treeselect
          :defaultExpandLevel=1
          v-model="queryParams.storeId"
          :options="storeOptions"
          :normalizer="normalizer"
          placeholder="请输入或选择门店名称"
          style="width: 350px"/>
      </el-form-item>
      <el-form-item label="作品类型" prop="opusType">
        <el-select v-model="queryParams.opusType" placeholder="请选择作品类型" clearable size="small">
          <el-option
            v-for="dict in opusTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain

          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['technicianOpus:technicianOpus:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain

          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['technicianOpus:technicianOpus:export']"
          v-if="false"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :height="tableHeight" :data="technicianOpusList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="技师头像" align="center" prop="techImage" >
        <template width="120" slot-scope="scope">
          <img style="width:60px;height:60px;border:none;" :src="scope.row.techImage">
        </template>
      </el-table-column>
      <el-table-column label="技师名称" align="center" prop="techName" />
      <el-table-column label="作品名称" align="center" prop="opusName" />
      <el-table-column label="作品类型" align="center" prop="opusType">
        <template slot-scope="scope">
          <dict-tag :options="opusTypeOptions" :value="scope.row.opusType"/>
        </template>
      </el-table-column>
      <el-table-column label="图片地址/预览图地址" align="center" prop="opusUrl" >
        <template width="120" slot-scope="scope">
          <img style="width:60px;height:60px;border:none;" :src="scope.row.opusUrl">
        </template>
      </el-table-column>
      <el-table-column label="视频地址" align="center" prop="opusVideoUrl" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"
            v-hasPermi="['technicianOpus:technicianOpus:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleDelete(scope.row)"
            v-hasPermi="['technicianOpus:technicianOpus:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或查看技师作品对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="技师头像">
          <imageScan v-model="form.techImage"/>
        </el-form-item>
        <el-form-item label="技师名称" prop="techName">
          <el-input v-model="form.techName" placeholder="请输入技师名称" />
        </el-form-item>
        <el-form-item label="作品名称" prop="opusName">
          <el-input v-model="form.opusName" placeholder="请输入作品名称" />
        </el-form-item>
        <el-form-item label="图片/缩略图" prop="opusInfo">
          <imageScan v-model="form.opusUrl"/>
        </el-form-item>
        <el-form-item label="视频地址" prop="opinionVideo" v-if="videosOpen">
          <VideoScan :list="videos" ref="videoScan" v-if="open"></VideoScan>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTechnicianOpus, getTechnicianOpus, logicDelTechnicianOpus, addTechnicianOpus, updateTechnicianOpus, exportTechnicianOpus } from "@/api/technicianOpus/technicianOpus";
import { storeListParam } from "@/api/store/store";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "TechnicianOpus",
  components:{Treeselect},
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 技师作品表格数据
      technicianOpusList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      videosOpen:true,

      // 作品类型字典
      opusTypeOptions: [],
      //门店选择器
      storeOptions:[],
      storePlaceholder:'请输入或者选择门店',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId: null,
        techId: null,
        techImage: null,
        techName: null,
        opusName: null,
        opusInfo: null,
        opusType: null,
        opusUrl: null,
        opusVideoUrl: null,
      },
      videos:null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  beforeRouteEnter(to, form, next) {
    next(vm => {
      const techId = vm.$route.query && vm.$route.query.id;
      vm.queryParams.techId = techId
      vm.getList();
    });
  },
  created() {

    this.calHeight()
      this.getDicts("source_type").then(response => {
      this.opusTypeOptions = response.data;
    });
    storeListParam().then(res =>{
      this.storeOptions = res
    })
  },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    normalizer(node) {
      return {
        id: node.dictValue,
        label: node.dictLabel,
      };
    },
    calHeight () {
          this.$nextTick(() => {
              const rect = this.$refs.appcontainer.getBoundingClientRect();
              let height= this.$refs.queryForm.$el.offsetHeight;
              this.tableHeight = rect.height-height-120;
          })
      },
    /** 查询技师作品列表 */
    getList() {
      this.loading = true;
      listTechnicianOpus(this.queryParams).then(response => {
        this.technicianOpusList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeId: null,
        techId: null,
        techImage: null,
        techName: null,
        opusName: null,
        opusInfo: null,
        opusType: null,
        opusUrl: null,
        opusVideoUrl: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加技师作品";
    },
    /** 查看按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTechnicianOpus(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.videosOpen = true
        this.videos = response.data.opusVideoUrl.split(",")
        if(this.videos.length == 1){
          if(!this.videos[0].trim()){
            this.videosOpen = false
          }
        }
        this.title = "查看技师作品";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTechnicianOpus(this.form).then(response => {
              this.msgSuccess("查看成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTechnicianOpus(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelTechnicianOpus(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有技师作品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportTechnicianOpus(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
